﻿using System;
using System.Data;

public partial class Bank : System.Web.UI.Page
{
    string mTableName = "bank";
    private readonly string mCurrentModule = Accounting.Module.ID;
    private readonly string mPageName = "Lista bancilor";
    private string appPath = string.Empty;

    bool allowLoanView = false;
    bool allowLoanEdit = false;

    protected void Page_Load(object sender, EventArgs e)
    {     
        appPath = Utils.GetApplicationPath(Request);
        Utils.GetMaster(this).PerformPreloadActions(mCurrentModule, mPageName);

        try
        {
            allowLoanView = Utils.PermissionAllowed(mCurrentModule, Accounting.Domains.Administration.Name, Constants.Constants.Classifiers.Permissions_View);
            allowLoanEdit = Utils.PermissionAllowed(mCurrentModule, Accounting.Domains.Administration.Name, Constants.Constants.Classifiers.Permissions_Edit);

            if (allowLoanView)
            {
                if (!IsPostBack)
                {
                    ShowPanel(allListPanel.ID);
                }
            }
            else
            {
                Response.Redirect(Utils.GetApplicationPath(Request) + "/AccessDenied.aspx");
            }
        }
        catch (Exception ex)
        { Utils.InfoText(this, "Eroare in System!", ex.Message.ToString()); }  
    }

    private void ShowPanel(string panelName)
    {
        #region Hide panels

        allListPanel.Visible = false;
        editEntryPanel.Visible = false;
        addNewEntryPanel.Visible = false;

        #endregion Hide panels

        try
        {
            #region Get Panel Name

            switch (panelName)
            {
                case "allListPanel":
                    allListPanel.Visible = true;

                    DataTable sourceDataTable = Utils.ModuleAccounting().GetBankList();
                    listGridView.DataSource = sourceDataTable;
                    listGridView.DataBind();
                    break;

                case "addNewPanel":
                    addNewEntryPanel.Visible = true;
                    break;

                case "editPanel":
                    editEntryPanel.Visible = true;
                    break;               

                case "addNewEntryPanel":
                    addNewEntryPanel.Visible = true;
                    break;

                case "editEntryPanel":
                    editEntryPanel.Visible = true;
                    break;
            }

            #endregion Get Panel Name

        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Atentie! Eroare in sistem!", ex.Message);
        }
    }

    
    protected void newButton_Click(object sender, EventArgs e)
    {
        addNewTextBox.Text = string.Empty;
        ShowPanel(addNewEntryPanel.ID);
    }

    protected void addNewCancelButton_Click(object sender, EventArgs e)
    {
        addNewTextBox.Text = string.Empty;
        ShowPanel(allListPanel.ID);
    }

    protected void addNewSaveButton_Click(object sender, EventArgs e)
    {
        try
        {
            string name = addNewTextBox.Text.Trim();
            string iban = ibanTextBox.Text.Trim();
            string correspondentAccount = correspondentAccountTextBox.Text.Trim();

            if (Utils.ModuleAccounting().AddBank(name, iban, correspondentAccount))
            {
                addNewTextBox.Text = string.Empty;
                ibanTextBox.Text = string.Empty;
                correspondentAccountTextBox.Text = string.Empty;
                ShowPanel(allListPanel.ID);
            }
            else
            {
                Utils.InfoText(this, "Atentie", " { " + name + " } - nu a fost salvat. Verificati conectiunea cu internet ");
            }
        }
        catch (Exception ex)
        { Utils.InfoText(this, "Eroare in System!", ex.Message.ToString()); }
    }

    protected void listGridView_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        int index = e.NewEditIndex;

        bankIDLabel.Text = listGridView.Rows[index].Cells[0].Text;
        editNameTextBox.Text = listGridView.Rows[index].Cells[1].Text;
        editIBANTextBox.Text = listGridView.Rows[index].Cells[2].Text;
        editCorrespondentAcounttextBox.Text = listGridView.Rows[index].Cells[3].Text;

        ShowPanel(editEntryPanel.ID);
    }

    protected void editSaveButton_Click(object sender, EventArgs e)
    {
        try
        {
            string strTypeID = bankIDLabel.Text;
            int bankID = 0;
            int.TryParse(strTypeID, out bankID);

            string name = editNameTextBox.Text.Trim();
            string iban = editIBANTextBox.Text.Trim();
            string correspondentAccount = editCorrespondentAcounttextBox.Text.Trim();

            if (Utils.ModuleAccounting().UpdateBank(bankID, name, iban, correspondentAccount))
            {
                addNewTextBox.Text = string.Empty;
                editIBANTextBox.Text =string.Empty;
                editCorrespondentAcounttextBox.Text = string.Empty;
                ShowPanel(allListPanel.ID);
            }
            else
            {
                Utils.InfoText(this, "Atentie", " { " + name + " } - nu a fost salvat. Verificati conectiunea internetului ");
            }
        }
        catch (Exception ex)
        { Utils.InfoText(this, "Eroare in System!", ex.Message.ToString()); }
    }

    protected void listGridView_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        int index = e.RowIndex;

        try
        {
            string strBankID = listGridView.Rows[index].Cells[0].Text;
            int bankID = 0;
            int.TryParse(strBankID, out bankID);

            if (Utils.ModuleAccounting().DeleteBank(bankID))
            {
                ShowPanel(allListPanel.ID);
            }
            else
            {
                Utils.InfoText(this, "Atentie", " Stergerea bancii nu a fost efectuata. Verificati conectiunea internetului ");
            }
        }
        catch (Exception ex)
        { Utils.InfoText(this, "Eroare in System!", ex.Message.ToString()); }
    }

    protected void editCancelButton_Click(object sender, EventArgs e)
    {
        addNewTextBox.Text = string.Empty;
        
        ShowPanel(allListPanel.ID);
    }

    protected void listGridView_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        listGridView.PageIndex = e.NewPageIndex;
        ShowPanel(allListPanel.ID);
    }
}